/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import au.com.bytecode.opencsv.CSVReader;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerFieldManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.ExcelUtil;
/*     */ import com.newcrm.util.MD5;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.util.XMLUtil;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.File;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class CustomerImportStep2 extends AbstractAction
/*     */ {
/*  45 */   private static Logger logger = Logger.getLogger(CustomerImportStep2.class
/*  46 */     .getName());
/*     */ 
/*  48 */   private String path = null;
/*     */ 
/*     */   public CustomerImportStep2(AbstractHttpServlet servlet) {
/*  51 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public CustomerImportStep2(AbstractHttpServlet servlet, String path) {
/*  55 */     super(servlet);
/*  56 */     this.path = path;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  63 */     User user = super.getLoginUser(request);
/*     */ 
/*  65 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/*  66 */     ServletFileUpload upload = new ServletFileUpload(fac);
/*  67 */     upload.setHeaderEncoding("utf-8");
/*  68 */     List fileList = null;
/*     */     try {
/*  70 */       fileList = upload.parseRequest(request);
/*     */     } catch (FileUploadException e) {
/*  72 */       logger.warn("upload file failed", e);
/*  73 */       return;
/*     */     }
/*     */ 
/*  76 */     Iterator it = fileList.iterator();
/*  77 */     String visible = "";
/*  78 */     String permissionsId = "";
/*  79 */     String groupPermissionsId = "";
/*  80 */     String fileType = "";
/*  81 */     String filePath = "";
/*  82 */     int totalNum = 0;
/*     */ 
/*  84 */     while (it.hasNext()) {
/*  85 */       FileItem item = (FileItem)it.next();
/*  86 */       if (item.isFormField()) {
/*  87 */         String id = item.getFieldName();
/*  88 */         if ("visible".equals(id)) {
/*  89 */           visible = Util.getURLParameter(item.getString());
/*     */         }
/*  92 */         else if ("permissionsId".equals(id)) {
/*  93 */           if (permissionsId.trim().length() == 0)
/*  94 */             permissionsId = Util.getURLParameter(item.getString());
/*     */           else {
/*  96 */             permissionsId = permissionsId + "," + 
/*  97 */               Util.getURLParameter(item.getString());
/*     */           }
/*     */ 
/*     */         }
/* 101 */         else if ("groupPermissionsId".equals(id)) {
/* 102 */           groupPermissionsId = Util.getURLParameter(item.getString());
/*     */         }
/* 105 */         else if ("fileType".equals(id)) {
/* 106 */           fileType = Util.getURLParameter(item.getString());
/*     */         }
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 112 */         String fileName = item.getName();
/* 113 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*     */         {
/* 116 */           request.setAttribute("fileName", fileName);
/*     */ 
/* 119 */           String extName = "";
/* 120 */           if (fileName.lastIndexOf(".") >= 0) {
/* 121 */             extName = fileName.substring(fileName.lastIndexOf("."));
/*     */           }
/* 123 */           filePath = MD5.getMD5(new StringBuilder(String.valueOf(user.userId)).append(System.currentTimeMillis()).toString()) + 
/* 124 */             extName;
/* 125 */           File file = new File(this.path, filePath);
/* 126 */           CSVReader csvReader = null;
/*     */           try {
/* 128 */             item.write(file);
/*     */ 
/* 130 */             List result = null;
/*     */ 
/* 132 */             if ("excel".equals(fileType)) {
/* 133 */               result = ExcelUtil.readExcel(file);
/* 134 */             } else if ("csv".equals(fileType)) {
/* 135 */               csvReader = new CSVReader(new FileReader(file));
/* 136 */               result = csvReader.readAll();
/*     */             }
/* 138 */             if (result != null) {
/* 139 */               totalNum = result.size();
/* 140 */               if (totalNum > 0)
/* 141 */                 request.setAttribute("totalNum", Integer.valueOf(totalNum - 1));
/*     */               else {
/* 143 */                 request.setAttribute("totalNum", Integer.valueOf(0));
/*     */               }
/* 145 */               if (totalNum > 10)
/* 146 */                 request.setAttribute("data", result.subList(0, 
/* 147 */                   9));
/*     */               else
/* 149 */                 request.setAttribute("data", result);
/*     */             }
/*     */           }
/*     */           catch (Exception e) {
/* 153 */             if (!"excel".equals(fileType)) {
/* 154 */               logger.warn("import failed", e);
/* 155 */               request.getRequestDispatcher(
/* 156 */                 "/customer_import_formatError.jsp").forward(
/* 157 */                 request, response);
/* 158 */               return;
/*     */             }
/*     */ 
/*     */             try
/*     */             {
/* 163 */               List result = XMLUtil.readXML(file);
/* 164 */               if (result != null) {
/* 165 */                 totalNum = result.size();
/* 166 */                 request.setAttribute("totalNum", Integer.valueOf(totalNum));
/* 167 */                 if (totalNum > 10)
/* 168 */                   request.setAttribute("data", result.subList(0, 
/* 169 */                     9));
/*     */                 else {
/* 171 */                   request.setAttribute("data", result);
/*     */                 }
/*     */               }
/*     */ 
/* 175 */               fileType = "xml";
/*     */             } catch (Exception e1) {
/* 177 */               logger.warn("import failed", e);
/* 178 */               logger.warn("import failed", e1);
/* 179 */               request.getRequestDispatcher(
/* 180 */                 "/customer_import_formatError.jsp").forward(
/* 181 */                 request, response);
/*     */ 
/* 185 */               if (csvReader != null)
/*     */                 try {
/* 187 */                   csvReader.close();
/*     */                 } catch (IOException se) {
/* 189 */                   logger.warn("close csv reader failed", se);
/*     */                 }
/* 182 */               return;
/*     */             }
/*     */ 
/* 185 */             if (csvReader != null)
/*     */               try {
/* 187 */                 csvReader.close();
/*     */               } catch (IOException se) {
/* 189 */                 logger.warn("close csv reader failed", e);
/*     */               }
/*     */           }
/*     */           finally
/*     */           {
/* 185 */             if (csvReader != null) {
/*     */               try {
/* 187 */                 csvReader.close();
/*     */               } catch (IOException e) {
/* 189 */                 logger.warn("close csv reader failed", e);
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 197 */     if ((Util.str2Int(visible) == 4) && 
/* 198 */       (permissionsId != null) && (permissionsId.length() > 0)) {
/* 199 */       UserManager userManager = new UserManager();
/* 200 */       String extValue = null;
/*     */       try {
/* 202 */         extValue = userManager.getExtInfoByType(permissionsId, 
/* 203 */           2);
/*     */       } catch (Exception e) {
/* 205 */         logger.warn("get max company num failed", e);
/*     */       }
/* 207 */       int maxCompanyNum = Util.str2Int(extValue);
/* 208 */       if (maxCompanyNum > 0) {
/* 209 */         CustomerManager customerManager = new CustomerManager();
/* 210 */         int currentCompanyNum = customerManager
/* 211 */           .getUserCompanyNum(permissionsId);
/* 212 */         if (maxCompanyNum < currentCompanyNum + totalNum) {
/* 213 */           request.setAttribute("currentNum", Integer.valueOf(currentCompanyNum));
/* 214 */           request.setAttribute("addNum", Integer.valueOf(totalNum));
/* 215 */           request.setAttribute("maxNum", Integer.valueOf(maxCompanyNum));
/* 216 */           request.getRequestDispatcher(
/* 217 */             "/company_new_error_overMax.jsp").forward(request, 
/* 218 */             response);
/* 219 */           return;
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 224 */     request.setAttribute("visible", visible);
/* 225 */     if (Util.str2Int(visible) == 4)
/*     */     {
/* 227 */       request.setAttribute("permissionsId", permissionsId);
/*     */     }
/* 229 */     else request.setAttribute("permissionsId", groupPermissionsId);
/*     */ 
/* 231 */     request.setAttribute("filePath", filePath);
/* 232 */     request.setAttribute("fileType", fileType);
/* 233 */     CustomerFieldManager manager = new CustomerFieldManager();
/*     */     try {
/* 235 */       List fields = manager.listAll(user.companyId);
/* 236 */       request.setAttribute("fields", fields);
/*     */     } catch (DBException e) {
/* 238 */       logger.warn("get fieldd failed", e);
/*     */     } catch (SQLException e) {
/* 240 */       logger.warn("get fieldd failed", e);
/*     */     }
/* 242 */     request.getRequestDispatcher("/customer_import_fieldMatch.jsp")
/* 243 */       .forward(request, response);
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.CustomerImportStep2
 * JD-Core Version:    0.6.1
 */